﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
using TeBigCommonUI.Common;

namespace TeBigCommonUI.Converter
{
    public class ChartColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(targetType == typeof(Brush)) || !(value is Color))
            {
                return null;
            }
            Color color = (Color)value;
            Brush brush = null;

            if (parameter is string)
            {
                brush = new SolidColorBrush(ColorHelper.CreateColorFromString(parameter as string));
            }
            if (parameter is Color)
            {
                brush = new SolidColorBrush((Color)parameter);
            }
            if (parameter is Brush)
            {
                brush = (parameter as Brush);
            }
            if (brush == null)
            {
                return new SolidColorBrush(color);
            }

            return ColorHelper.MixColors(ColorHelper.NormalizeGradients(brush), color);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ChartGradientConverter : IValueConverter
    {
        public bool IsNumericType(object o)
        {
            switch (Type.GetTypeCode(o.GetType()))
            {
                case TypeCode.Byte:
                case TypeCode.SByte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.Decimal:
                case TypeCode.Double:
                case TypeCode.Single:
                    return true;

                default:
                    return false;
            }
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(targetType == typeof(Brush)) || !(value is Color))
            {
                return null;
            }

            double brightness = -1;

            if (parameter is string && double.TryParse(parameter as string, out double bri))
            {
                brightness = bri;
            }
            else if (IsNumericType(parameter))
            {
                brightness = (double)parameter;
            }

            Color oriColor = (Color)value;

            var fromColor = new HLSColor(oriColor);
            var toColor = new HLSColor(oriColor);

            //var oriBrightness = ColorHelper.GetBrightness(oriColor);
            //var s = ColorHelper.GetSaturation(oriColor);
            //var h = ColorHelper.GetHue(oriColor);

            //Color fromColor = oriColor;
            //Color toColor = oriColor;

            //if (s < 0.5)
            //{
            //    fromColor = ColorHelper.Brightness(oriColor, 0.4);
            //    toColor = ColorHelper.Brightness(oriColor, 0.4);
            //}

            return new LinearGradientBrush(fromColor.ToColor(180, 150), toColor.ToColor(160, 150), 90);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}